from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

# Create your views here.
from rest_framework.viewsets import ModelViewSet
from job import models
from job.ser import DictSer,PositionSer
from rest_framework.pagination import PageNumberPagination
from django_filters import FilterSet
import django_filters
class Page(PageNumberPagination):
    page_size = 2
    page_size_query_param = 'size'
class Filters(FilterSet):
    sn=django_filters.CharFilter(field_name='sno',lookup_expr='contains')
    name=django_filters.CharFilter(field_name='name',lookup_expr='contains')
    class Meta:
        model = models.Position
        fields = ['type']
class DictView(ModelViewSet):
    queryset = models.Dict.objects.all()
    serializer_class = DictSer
class PositionView(ModelViewSet):
    queryset = models.Position.objects.filter(isdel=False)
    serializer_class = PositionSer
    pagination_class = Page
    filter_backends = [django_filters.rest_framework.DjangoFilterBackend]
    filterset_class = Filters
class DelUpdate(APIView):
    def put(self,request):
        id=request.query_params.get('id')
        models.Position.objects.filter(id=id).update(isdel=True)
        # ser=PositionSer(instance=models.Position.objects.filter(id=id).first())
        return Response({'msg':'ok'})

class DelUpdate2(APIView):
    def delete(self,request):
        id=request.query_params.get('id')
        if models.Position.objects.filter(id=id).update(isdel=True):
            models.Position.objects.get(id=id).delete()
            return Response({'msg':'ok'})